package com.share.zabbix.method;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.share.zabbix.request.ZabbixRequest;
import com.share.zabbix.request.ZabbixRequestBuilder;
import com.share.zabbix.response.ZabbixResponseEvent;

import cn.hutool.core.date.DateTime;

public class ZabbixEventMethod extends ZabbixBaseMethod{
	
	public static List<ZabbixResponseEvent> getEvent(Date begin, Date end, List<String> hostids, List<String> groupids) {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder().method("event.get")
				.extendOutput()
				.timeFrom(begin)
				.timeTill(end)
				.paramEntry("selectHosts", Arrays.asList("name", "host"))
				.paramEntry("select_acknowledges", "extend")
				.sortByDesc("clock", "eventid")
				.build();
		if(hostids != null)
			request.putParam("hostids", hostids);
		if(groupids != null)
			request.putParam("groupids", groupids);
		try {
			JSONObject response = call(request);
			Gson gson = new GsonBuilder().create();
			List<ZabbixResponseEvent> hisls = gson.fromJson(response.getJSONArray(ZABBIX_KEY_RESULT).toString(),
					new TypeToken<ArrayList<ZabbixResponseEvent>>() {
					}.getType());

			return hisls;
		} catch (Exception e) {
			log.error(e);
			log.info("Zabbix查询event失败！");
		}
		return null;
	}
	
	public static Object ackEvent(String eventid, String message) {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder().method("event.acknowledge")
				.paramEntry("eventids", eventid)
				.paramEntry("action", 2)
				.paramEntry("message", message)
				.build();
		try {
			JSONObject response = call(request);
			Gson gson = new GsonBuilder().create();
			List<ZabbixResponseEvent> hisls = gson.fromJson(response.getJSONArray(ZABBIX_KEY_RESULT).toString(),
					new TypeToken<ArrayList<ZabbixResponseEvent>>() {
					}.getType());

			return hisls;
		} catch (Exception e) {
			log.error(e);
			log.info("Zabbix事件确认失败！");
		}
		return null;
	}
}
